package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

/**
 * 后台初始化超级管理员
 * ApplicationListener ServetRequest监听应用环境的变化,项目启动时创建超级管理员
 */
@Service
public class InitAdmin implements ApplicationListener<ContextRefreshedEvent>{
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (loginInfoMapper.selectByUsername("admin") > 0) {
            return;
        }
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername("admin");
        loginInfo.setPassword(MD5.encode("000000admin"));
        loginInfo.setUserType(LoginInfo.USERTYPE_MGR);
        loginInfoMapper.insert(loginInfo);
    }
}
